'use strict';

const Controller = require('../core/base_controller');

class Role extends Controller {

	async list() {

		const { app, ctx } = this;

		const res = await ctx.service.role.list();

		this.success(res);
	}

	
	async create () {
		const { app, ctx } = this;

		const {id, code, name, del } = ctx.request.body;

		let res = undefined;
		
		if(id == undefined){
			//创建
			res = await ctx.service.role.create(code, name);
		}else{
			if(del == undefined){
				//修改
				res = await ctx.service.role.update(name, code, id);
			}else{
				//删除
				res = await ctx.service.role.delete(id);
			}
		}

		this.success(res);
 	}

 	//为角色设置菜单。
 	async setMenu () {
 		const { app, ctx } = this;
 		const { roleId, menuIdsChecked, menuIdsUnchecked } = ctx.request.body;

 		if(roleId == '' || menuIdsChecked == '') {
 			this.error(22, '请选择菜单');
 			return;
 		}

 		const res = await ctx.service.role.setMenu(roleId, menuIdsChecked, menuIdsUnchecked);

 		console.log(res);

 		this.success(res);

 	}
}

module.exports = Role;
